﻿using DDD.Domain.Abstract.DBContext;
using DDD.Domain.Entities;
using DDD.Infrastructure.Configurations;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace DDD.Infrastructure.DBContext
{
    public sealed class DDDDomainDbContext : DomainDBContext
    {
        private readonly IMediator _mediator;
        private readonly IConfiguration _configuration;

        public DDDDomainDbContext() { }

        public DDDDomainDbContext(DbContextOptions options, IMediator mediator, IConfiguration configuration) : base(options, mediator)
        {
            _mediator = mediator;
            _configuration = configuration;
        }

        public DbSet<User> User { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new UserConfiguration());

            base.OnModelCreating(modelBuilder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                string connectionString = "Server=127.0.0.1;Database=DDD;Port=3306;charset=utf8;uid=root;pwd=123456;";

                optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
            }

            base.OnConfiguring(optionsBuilder);
        }

    }
}
